<?php

namespace App\Controller\Back;

use App\Base\BaseController4Back;
use App\Entity\AdminGroup;
use App\Entity\AdminMenu;
use App\Model\AdminMenuModel;
use App\Repository\AdminUserRepository;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;

/**
 * @Route("/back")
 */
class FrameController extends BaseController4Back
{
    /**
     * @Route("/login", name="BackLogin")
     */
    public function BackLogin()
    {
        if (!$msg = $this->getParamRequired("msg")) {$msg = "";}
        $data=["msg" => $msg];
        return $this->show("login",$data);
    }

    /**
     * @Route("/login/do", name="BackLoginDo")
     */
    public function BackLoginDo(AdminUserRepository $repository)
    {
        $name = $this->getParamRequired("name");
        $password = $this->getParamRequired("password");
        $admin = $repository->findOneBy(array("name"=>$name,"password"=>$password));
        if (!$admin) {
            //账号或者密码错误
            return $this->redirectToRoute("BackLogin",array("msg"=>"账号或密码错误"));
        }else{
            $roles = $admin->getGroups()->getValues();
            $sessRole = [];
            foreach ($roles as $role) {
                $sessRole[] = $role->getRole();
            }
            $this->mySession()->set("role",$sessRole);
            $this->mySession()->set("id",$admin->getId());
            return $this->redirectToRoute("BackIndexFrame");
        }
    }

    /**
     * @Route("/index", name="BackIndex")
     */
    public function BackIndex(AdminUserRepository $repository)
    {
        $data=[];
        $data["uri"] = "";
        return $this->show("index",$data);
    }

    /**
     * @Route("/frame", name="BackIndexFrame")
     */
    public function BackIndexFrame(AdminUserRepository $repository,SerializerInterface $serializer)
    {
        $uid = $this->needToBeAdmin();
        $admin = $repository->find($uid);

        $roles = $admin->getGroups()->getValues();
//        dd($roles);
        $got = [];
        foreach ($roles as $role) {
            /**@var $role AdminGroup*/
            $menus = $role->getMenus()->getValues();
//            dd($menus);
            foreach ($menus as $menu) {
                $got[] = $menu->getId();
            }
        }
//        dd($got);
        $data=[];

        //按钮功能应该在用户登录时确定
        $AdminMenuModel = new AdminMenuModel($this->getDoctrine());
        $menuTree = $AdminMenuModel->getRootAdminMenu();
        $temp = [];

        $this->serializer = $serializer;
        $menuTree = $this->getSons($menuTree,$got);
//        dd($menuTree = $this->getSons($menuTree,$got));
        $data["menuTree"] = $menuTree;
        $data["admin"] = $admin;


        return $this->show("layout.frame",$data);
    }

    public function getSons($array,$ids)
    {
        $tem = [];
        foreach ( $array as $item){
            /**@var $item AdminMenu*/
            if (in_array($item->getId(),$ids)) {
                $sons = $item->getSons()->getValues();
//                dump($item);
                $arr = $this->serializer->normalize($item,"array");
//                $arr = $this->toArray($item);
                $arr["sons"] = $this->getSons($sons,$ids);
                $tem[] = $arr;
            }
        }
        return $tem;
    }


    /**
     * @Route("/welcome", name="BackFrameWelcome")
     */
    public function BackFrameWelcome(AdminUserRepository $repository)
    {
        $data=[];
        return $this->show("welcome",$data);
    }

    /**
     * @Route("/logout", name="BackFrameLogout")
     */
    public function BackFrameLogout(AdminUserRepository $repository)
    {
        $this->mySession()->clear();
        return $this->redirectToRoute("BackLogin");
    }




}
